package cn.training.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.controller.HelloWorldController;
import cn.training.bean.UserBean;
import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;
@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Model model) {
		UserBean userBean=new UserBean();
		model.addAttribute("userBean",userBean);
		return "helloWorld";
	}

	@RequestMapping(value = "/init", method = RequestMethod.POST, params = "login")
	public String initLogin(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException {
     if (results.hasErrors()) {
		
			return "helloWorld";
			}
		UserBean result = helloWorldService.searchUser(userBean);
	
		if (result != null) {
			List<UserBean> result1 = helloWorldService.searchUsers();
			model.addAttribute("userBean", result1);
			return "login";
		} else {
			model.addAttribute("message", "用户名或密码错误");
		}
		return "helloWorld";
	}

	@RequestMapping(value = "/init", method = RequestMethod.POST, params = "register")
	public String initRegister(UserBean userBean, Model model) {
		

		return "register";
	}

	@RequestMapping(value = "/init1", method = RequestMethod.POST, params = "add")
	public String initOnsert(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			return "register";
		}
		String pass1 = userBean.getPassword();
		String pass2 = userBean.getPasswordConfirm();
		model.addAttribute("userBean", userBean);
		if (pass1.equals(pass2)) {
			int insert = helloWorldService.insert(userBean);
			if (insert == 1) {
				model.addAttribute("message1", "注册成功");
				return "registerResult";
			}else if(insert==888){
				model.addAttribute("message1", "数据库中已经存在");
				return "register";

			}{
				model.addAttribute("message1", "注册失败");
				return "register";
			}

		} else {
			model.addAttribute("message1", "两次输入密码不同");
			return "register";

		}
		

	}

	@RequestMapping(value = "/init1", method = RequestMethod.POST, params = "back")
	public String initInsert(UserBean userBean, Model model) {

		return "helloWorld";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deleteLogin(UserBean userBean, Model model) {
      //  int result=helloWorldService.deleteUser(userBean);
       // List<UserBean> result = helloWorldService.searchUserss(userBean);
       // model.addAttribute("list", result);
		return "del";
	}
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String updateLogin(UserBean userBean, Model model) {
		
		return "xiugaijieguo";
	}
	
	@RequestMapping(value = "/init2", method = RequestMethod.POST, params = "querenshanchu")
	public String initquerenshanchu(UserBean userBean, Model model) {
		   int result=helloWorldService.searchdelete(userBean);
	        List<UserBean> refresh = helloWorldService.searchUserss(userBean);
	        model.addAttribute("userBean", refresh);
			return "login";
		
}
	@RequestMapping(value = "/init2", method = RequestMethod.POST, params = "quxiaoshanchu")
	public String initquxiaoshanchu(UserBean userBean, Model model) {

		 //int result=helloWorldService.deleteUser(userBean);
	        List<UserBean> refresh = helloWorldService.searchUserss(userBean);
	        model.addAttribute("userBean",refresh);
			return "login";
			
	}
	
	

	@RequestMapping(value = "/init4", method = RequestMethod.POST)
	public String initxiugai(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			return "xiugaijieguo";
		}
		
			int update = helloWorldService.updateUser(userBean);
			
			if (update==1) {
			     List<UserBean> refresh = helloWorldService.searchUserss(userBean);
			        model.addAttribute("userBean",refresh);
					return "login";
		} else {
			model.addAttribute("message2", "修改失败");
			model.addAttribute("userBean",userBean);
			return "xiugaijieguo";
		}
		
	}
	

}
